
<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd)
	{
		case 1:
			//get one promotion based on id
			get_promotion();
			break;
			
		case 2:
			save();
			break;
			
		case 3:
			get_all_health_promo();
			break;
			
		case 4:
			delete();
			break;

        case 5:
            add();
            break;
			
		default:
			echo "{";
			echo jsonn("result", 0).",";
			echo jsons("message", "unknown command");
			echo "}";
		
	}
	
	function get_promotion()
	{
		include_once("health_promo_class.php");
		$id = get_datan("idhealth_promotion");
		$v=new health_promo_class();
		$v->get_promo($id);
		$row = $v->fetch();
		if(!$row)
		{
			echo "{";
			echo jsonn("result", 0).",";
			echo jsons("message", "health promotion not found");
			echo "}";
			return;
		}
		echo "{";
			echo jsonn("result",1) .",";
			echo '"health_promotion":{';
			echo jsonn("id",$id).",";
			echo jsons("venue",$row["venue"]). ",";
            echo jsons("pdate",$row["date"]). ",";
            echo jsons("topic",$row["topic"]). ",";
			echo jsons("method",$row["method"]). ",";
			echo jsons("target audience",$row["target_audience"]). ",";
            echo jsonn("number_of_audience",$row["number_of_audience"]). ",";
			echo jsons("remarks",$row["remarks"]). ",";
			echo jsons("month",$row["month"]). ",";
			echo jsonn("latitude",$row["latitude"]). ",";
			echo jsonn("longitude",$row["longitude"]). ",";
		    echo jsons("image",$row["image"]). ",";
		    echo jsonn("idcho",$row["idcho"]). ",";
			echo jsonn("subdistrict_id", $row["subdistrict_id"]);
			echo "}";
		echo "}";
	}
	
	function get_all_health_promo()
	{
		include_once("health_promo_class.php");
		$v=new health_promo_class();
		$row=$v->get_all_health_promo();
		if(!$row)
		{
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","health promotion not found");
			echo "}";
			return;
		}
		
		   echo "{";
			echo jsonn("result",1) .",";
			echo'"health_promotion":{[';
			promo($row, $v);
		    echo "]}";
		    echo "}";
	}
	
	function promo($row, $v)
	{
	    $row=$v->fetch();
	    while($row)
		{
		echo "{";
			echo jsonn("id",$row["idhealth_promotion"]).",";
			echo jsons("pdate",$row["date"]). ",";
			echo jsons("venue",$row["venue"]). ",";
			echo jsons("topic",$row["topic"]). ",";
			echo jsons("method",$row["method"]). ",";
			echo jsons("target audience",$row["target_audience"]). ",";
            echo jsonn("number_of_audience",$row["number_of_audience"]). ",";
			echo jsons("remarks",$row["remarks"]). ",";
			echo jsons("month",$row["month"]). ",";
			echo jsonn("latitude",$row["latitude"]). ",";
			echo jsonn("longitude",$row["longitude"]). ",";
		    echo jsons("image",$row["image"]). ",";
		    echo jsonn("idcho",$row["idcho"]). ",";
			echo jsonn("subdistrict_id", $row["subdistrict_id"]);
		echo "}";
		$row=$v->fetch();
		}
	}
		
	function save() 
	{
	 include_once("health_promo_class.php");
		$id=get_datan("idhealth_promotion");
		$date=get_data("date");
		$venue=get_data("venue");
		$topic=get_data("topic");
		$method=get_data("method");
		$target_audience=get_data("target_audience");
		$number_of_audience=get_datan("number_of_audience");
		$remarks=get_data("remarks");
		$month=get_data("month");
		$latitude=get_datan("latitude");
		$longitude=get_datan("longitude");
		$image=get_data("image");
		$idcho=get_datan("idcho");
		$subdistrict_id=get_datan("subdistrict_id");
	 
	 $vc=new health_promo_class();
	 $row=$vc->update_health_promo($id,$date,$venue,$topic,$method,$target_audience,
	 $number_of_audience,$remarks,$month,$latitude,$longitude,$image,$idcho,$subdistrict_id);
	 if(!$row)
	 	{
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","health promotion couldnt update");
			echo "}";
			return;
		}
        else {
            echo "{";
            echo jsonn("result", 1) . ",";
            echo jsons("message", "health promotion updated!");
            echo "}";
        }
		
	}
	
	function delete()
	{
	include_once("health_promo_class.php");
	$v=new health_promo_class();
	$id=get_datan("idhealth_promotion");
	if(!$id)
		{
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","ID not found");
			echo "}";
			return;
		}
	if(!$v->delete_health_promo($id))
		{
            echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","Delete wasn't successful");
			echo "}";
			return;	
	 
		}
	else
		{
	        echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","Delete was successful");
			echo "}";
			return;
		}
	}


    function add()
    {
        include_once("health_promo_class.php");

        $date=get_data("date");
        $venue=get_data("venue");
        $topic=get_data("topic");
        $method=get_data("method");
        $target_audience=get_data("target_audience");
        $number_of_audience=get_datan("number_of_audience");
        $remarks=get_data("remarks");
        $month=get_data("month");
        $latitude=get_datan("latitude");
        $longitude=get_datan("longitude");
        $image=get_data("image");
        $idcho=get_datan("idcho");
        $subdistrict_id=get_datan("subdistrict_id");

        $v=new health_promo_class();
        $dataset = $v->add_health_promo($date,$venue,$topic,$method,$target_audience,
            $number_of_audience,$remarks,$month,$latitude,$longitude,$image,$idcho,$subdistrict_id);
        if(!$dataset)
        {
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","Add not successful.");
            echo "}";
            return;
        }
        else {
            echo "{";
            echo jsonn("result", 1) . ",";
            echo jsons("message", "health promotion added!");
            echo "}";
        }
    }
		
	
	
			
	
			
		
	

?>

